package com.koron.ticket.utils;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.koron.common.core.business.print.bean.PrintTemplateBean;
import com.koron.common.core.business.print.printstandard.PrintStandardMapper;
import com.koron.common.core.config.ThreadLocalContext;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.ADOConnection;

import java.util.List;
import java.util.Optional;

public class PrintUtils {

    /**
     * 获取指定租户业务的打印模板
     * @param printbBsinessCode 业务打印标识
     * @param appCode           租户code
     * @return
     */
    public static String getPrintTemplateIdByName(String printbBsinessCode, String appCode) {
        List<PrintTemplateBean> templatelist = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), factory -> {
            List<PrintTemplateBean> templateListByBusinessCode = factory.getMapper(PrintStandardMapper.class).getTemplateListByBusinessCode(printbBsinessCode);
            return templateListByBusinessCode;
        }, List.class);

        Assert.isFalse(CollectionUtils.isEmpty(templatelist),"没有该业务的打印模板");
        Optional<PrintTemplateBean> first = templatelist.stream().filter(a -> StrUtil.isNotBlank(a.getAppCode()) && a.getAppCode().equals(appCode)).findFirst();
        return first.get().getId();
    }

    /**
     * 获取业务的默认打印模板
     * @param printbBsinessCode
     * @return
     */
    public static String getDefaultPrintTemplateId(String printbBsinessCode) {
        List<PrintTemplateBean> templatelist = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), factory -> {
            List<PrintTemplateBean> templateListByBusinessCode = factory.getMapper(PrintStandardMapper.class).getTemplateListByBusinessCode(printbBsinessCode);
            return templateListByBusinessCode;
        }, List.class);

        Assert.isFalse(CollectionUtils.isEmpty(templatelist),"没有该业务的打印模板");
        return templatelist.stream().filter(a -> a.getDefaultFlag()).findFirst().get().getId();
    }

}
